import { useSelector } from 'react-redux'
import type { RootState } from '../store/index'

// 权限常量
export const PERMISSIONS = {
  ADMIN: 1,
  USER: 2,
} as const

// 权限类型
export type Permission = typeof PERMISSIONS[keyof typeof PERMISSIONS]

// 权限控制Hook
export const usePermission = () => {
  const { userInfo, isLoggedIn } = useSelector((state: RootState) => state.user)
  
  // 获取当前用户角色
  const userRole = userInfo?.role || null
  
  // 检查是否为管理员
  const isAdmin = userRole === PERMISSIONS.ADMIN
  
  // 检查是否为普通用户
  const isUser = userRole === PERMISSIONS.USER
  
  // 检查是否有特定权限
  const hasPermission = (requiredRole: Permission): boolean => {
    if (!isLoggedIn || !userRole) return false
    return userRole <= requiredRole // 数字越小权限越高
  }
  
  // 检查是否可以访问语音对话功能
  const canUseVoiceChat = isAdmin
  
  // 检查是否可以访问AI聊天功能
  const canUseAIChat = isAdmin
  
  // 检查是否可以访问管理功能
  const canAccessAdmin = isAdmin
  
  // 检查是否可以观看视频
  const canWatchVideo = isLoggedIn && (isAdmin || isUser)
  
  // 检查是否可以进入展览馆
  const canEnterExhibition = isLoggedIn && (isAdmin || isUser)
  
  // 检查是否可以切换语言
  const canSwitchLanguage = isAdmin
  
  // 检查是否可以换肤
  const canChangeSkin = isAdmin
  
  return {
    userRole,
    isAdmin,
    isUser,
    isLoggedIn,
    hasPermission,
    canUseVoiceChat,
    canUseAIChat,
    canAccessAdmin,
    canWatchVideo,
    canEnterExhibition,
    canSwitchLanguage,
    canChangeSkin,
  }
}

export default usePermission
